package com.cyw.baselibrary.view.recycleViewdecoration;


import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;


/**
 * RecycleView分割线
 */
public class ArrayViewFactory {
    private Context activity;
    private LinearLayoutManager linearLayoutManager;
    private GridLayoutManager gridLayoutManager;
    private DividerLineDecoration dividerLineDecoration;

    private ArrayViewFactory(Context activity) {
        this.activity = activity;
    }

    public static ArrayViewFactory newInstance(Context activity) {
        return new ArrayViewFactory(activity);
    }

    public DividerLineDecoration initVerticalRvBase(RecyclerView rv) {
        linearLayoutManager = new LinearLayoutManager(activity);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv.setLayoutManager(linearLayoutManager);
        dividerLineDecoration = new DividerLineDecoration(activity, DividerLineDecoration.VERTICAL_LIST, true);
        rv.addItemDecoration(dividerLineDecoration);
        return dividerLineDecoration;
    }
    public void initHorizontalRvBase(RecyclerView rv) {
        linearLayoutManager = new LinearLayoutManager(activity);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rv.setLayoutManager(linearLayoutManager);
//        dividerLineDecoration = new DividerLineDecoration(activity, DividerLineDecoration.VERTICAL_LIST, false);
//        rv.addItemDecoration(dividerLineDecoration);
    }

    public void initGridRvBase(RecyclerView rv, int spanCount, int horizontalSpace, int verticalSpace) {
        rv.setLayoutManager(new GridLayoutManager(activity, spanCount));
        rv.addItemDecoration(new ItemGridDecoration(horizontalSpace, verticalSpace, spanCount));
    }



    public DividerLineDecoration initVerticalRvNoSpaceNoLoadMore(RecyclerView rv) {
        initVerticalRvBase(rv);
        rv.addItemDecoration(dividerLineDecoration);
        dividerLineDecoration.setSpace(0);
        return dividerLineDecoration;
    }



    public DividerLineDecoration initVerticalRvInsideScrollViewNotPage(RecyclerView rv) {
        linearLayoutManager = new LinearLayoutManager(activity);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        rv.setLayoutManager(linearLayoutManager);
        dividerLineDecoration = new DividerLineDecoration(activity, DividerLineDecoration.VERTICAL_LIST, true);
        rv.addItemDecoration(dividerLineDecoration);
        rv.setNestedScrollingEnabled(false);
        rv.setHasFixedSize(true);
        return dividerLineDecoration;
    }


    public LinearLayoutManager getLinearLayoutManager() {
        return linearLayoutManager;
    }

    public static boolean isViewTop(RecyclerView recyclerView) {
        boolean apply = false;
        RecyclerView.LayoutManager layout = recyclerView.getLayoutManager();
        LinearLayoutManager layoutManager = ((LinearLayoutManager) layout);
        int orientation = layoutManager.getOrientation();
        if (orientation == LinearLayoutManager.VERTICAL) {
            View child = layoutManager.getChildAt(0);
            if (child != null) {
                int position = recyclerView.getChildLayoutPosition(child);
                if (position == 0) {
                    apply = child.getTop() >= recyclerView.getTop();
                }
            } else {
                apply = true;
            }
        }
        return apply;
    }

}
